/**
 * related to modifyPassword.ui
 * 
 * @Author : 2037630@163.com
 * @Timestamp : 2017-04-19
 */
var Controller = require("core/controller");
var net = require('util/net')
var log = require("util/log")
var constant = require('config/constant')
var storage = require('util/storage')
var navigate = require('util/navigate')
var server = require('config/server')
var router = require('config/router')
var notify = sm('do_Notification')
var newPassword=ui("newPassword")
var repeatPassword=ui("repeatPassword")
var Algorithm = sm('do_Algorithm')
Controller({
	config:{
		name : "modifyPassword",
		header:{
			title:"修改密码",
			right:{
				text:'完成',
				handle:function(){
					this.testPassword();
				},
				scope:this
			}
		}
	},
	ui:{
		newPassword:"newPassword",
		repeatPassword:"repeatPassword",
		oldPassword:"oldPassword"
		
	},
	mapping:{
		"oldPassword.text":"old_password",
		"newPassword.text":"new_password",
		"repeatPassword.text":"repeat_password"
	},
	data:{
		"old_password":"",
		"new_password":"",
		"repeat_password":""
	},
	event:{
		
	},
	loaded: function(){
//		// 更新页面头部信息
//		this.updateHeader('修改密码');
		
	},
	methods:{
//		密码匹配操作
		testPassword:function(){
			log.info("888888")
			if(this.data.new_password!==this.data.repeat_password){
				notify.toast("两次密码不一致");
			}
			else if(!this.data.new_password||!this.data.repeat_password){
				notify.toast("请输入修改密码");
			}
			else if(this.data.old_password===""){ 
				notify.toast("请输入原始密码")
			}
			else{
				var self=this
				Algorithm.md5('string',self.data.old_password,function(old_password){
					self.data.old_password=old_password
					Algorithm.md5('string',self.data.new_password,function(new_password){
						self.data.new_password=new_password
						self.modifyPassword(self.data.old_password,self.data.new_password)
					})
				})	
			}
			log.info("end")
		},
//		修改密码请求
		modifyPassword:function(old_pass,new_pass){
			net.send({
				server : server.organization.modifyPassword,
				data : {
					oldPassword: old_pass,
					newPassword : new_pass,
				},
				success : function(data) {
					notify.toast("修改成功")
					navigate.back();
				},
				error : function(data) {
					notify.toast("修改失败")
				},
				scope:this
			})
		},
		
	}
})